//
//  FavViewController.m
//  iCarStyle
//
//  Created by liunian on 13-9-24.
//  Copyright (c) 2013年 liunian. All rights reserved.
//

#import "FavViewController.h"
#import "PPiFlatSegmentedControl.h"
#import "NSString+FontAwesome.h"
#import "ArticleCell.h"
#import "ArticleManager.h"
#import "GMGridView.h"
#import "FavoritCell.h"
#import "Favorit.h"
#import "DetailViewController.h"
#import "DataEnvironment.h"
#import "AppDelegate.h"

#define kDefaultBottomBarHeight           38

@interface FavViewController ()<UITableViewDataSource,UITableViewDelegate,ArticleManagerDelegate>{
    NSInteger   _viewIndex;
}
@property (nonatomic, assign) BOOL  dataEditing;
@property (nonatomic, assign) NSInteger   viewIndex;

@property (nonatomic, retain) UIScrollView  *scrollView;
@property (nonatomic, retain) UITableView  *tableView;
@property (nonatomic, retain) PPiFlatSegmentedControl *segmented;

@property (nonatomic, retain) NSMutableArray   *news;
@property (nonatomic, retain) NSMutableArray   *items;
@end

@implementation FavViewController

- (void)dealloc{
    [[ArticleManager sharedManager] removeObserver:self];
}
- (id)init
{
    self = [super init];
    if (self) {
        // Custom initialization
        self.title = @"收藏";
    }
    return self;
}
- (NSString *)tabTitle
{
	return self.title;
}
- (NSString *)tabImageName
{
	return @"image-7";
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self scrollView];
    self.navigationItem.titleView = self.segmented;
    [[ArticleManager sharedManager] addObserver:self];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.hidesBottomBarWhenPushed = NO;
    self.navigationController.navigationBarHidden = NO;
    AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
//    [appDelegate hideTabbar:NO];
    [[ArticleManager sharedManager] getArticles];
}
- (void)editChange{
    if (self.viewIndex == 0 && self.news.count) {
        self.dataEditing = !self.dataEditing;
    }else{
    }
    
    
    

}

- (void)endEditState{
    self.dataEditing = NO;
}

#pragma mark news
#pragma mark =======================================================================

#pragma mark ArticleManagerDelegate
- (void)mkManagerDidFinishedWithFavArticles:(NSMutableArray *)articles{
    if (articles) {
        [self.news removeAllObjects];
        [self.news addObjectsFromArray:articles];
        [self.tableView reloadData];
    }
}

#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.news.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"news";
    ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[ArticleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    Article *article = [self.news objectAtIndex:indexPath.row];
    [cell updateWithArticle:article];
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)index{
    if (editingStyle==UITableViewCellEditingStyleDelete) {
        Article *article = [self.news objectAtIndex:index.row];
        [[ArticleManager sharedManager] cancelFavArticle:article];
        [self.news removeObjectAtIndex:index.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation:UITableViewRowAnimationLeft];
    }
}
#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kHeightArticleCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    Article *article = [self.news objectAtIndex:indexPath.row];
    DetailViewController *detailVC = [[DetailViewController alloc] initWithArticle:article];
    detailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailVC animated:YES];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}
#pragma mark =======================================================================

- (void)viewUpdate{
    if (self.viewIndex) {
        self.title = @"商品收藏";
    }else{
        self.title = @"文章收藏";
    }
    self.dataEditing  = NO;
    [self.scrollView setContentOffset:CGPointMake(self.viewIndex * CGRectGetWidth(self.scrollView.frame), 0) animated:YES];
}

#pragma mark setter
- (void)setViewIndex:(NSInteger)viewIndex{
    if (_viewIndex != viewIndex) {
        _viewIndex = viewIndex;
        [self viewUpdate];
    }
}

- (void)setDataEditing:(BOOL)dataEditing{
    if (_dataEditing != dataEditing) {
        _dataEditing = dataEditing;
        [self editChange];
    }
}
#pragma mark - getter
- (PPiFlatSegmentedControl *)segmented{
    if (!_segmented) {
        _segmented = [[PPiFlatSegmentedControl alloc] initWithFrame:CGRectMake(100, 4, 160, 30) items:@[
                                                                                                         @{@"text":@"文章收藏"}
                                                                                                         ]
                                                       iconPosition:IconPositionRight andSelectionBlock:^(NSUInteger segmentIndex) {
                                                           BMLog(@"segmentIndex:%d",segmentIndex);
                                                           self.viewIndex = segmentIndex;
                                                       }];
        _segmented.color=[UIColor flatWhiteColor];
        _segmented.borderWidth=0.5;
        _segmented.borderColor=[UIColor flatBlackColor];
        _segmented.selectedColor=[UIColor flatBlackColor];
        _segmented.textAttributes=@{NSFontAttributeName:[UIFont systemFontOfSize:14],
                                    NSForegroundColorAttributeName:[UIColor flatBlackColor]};
        _segmented.selectedTextAttributes=@{NSFontAttributeName:[UIFont systemFontOfSize:14],
                                            NSForegroundColorAttributeName:[UIColor flatWhiteColor]};

    }
    return _segmented;
}

- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        [_scrollView setBackgroundColor:[UIColor clearColor]];
        [_scrollView setScrollEnabled:NO];
        [_scrollView setShowsHorizontalScrollIndicator:NO];
        [_scrollView setContentSize:CGSizeMake(2*CGRectGetWidth(self.view.bounds), CGRectGetHeight(_scrollView.frame))];
        [_scrollView setClipsToBounds:YES];
        [self.view addSubview:_scrollView];
    }
    return _scrollView;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.scrollView.bounds
                                                  style:UITableViewStylePlain];
        [_tableView setBackgroundColor:[UIColor whiteColor]];
        [_tableView setDataSource:self];
        [_tableView setDelegate:self];
        [self.scrollView addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)news{
    if (!_news) {
        _news = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _news;
}
@end
